package com.ww.editsms;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

import com.chinawangw.R;
import com.ww.editsms.adapter.PersonAdapter;
import com.ww.editsms.db.Cursordb;
import com.ww.editsms.entity.Person;
import com.ww.editsms.util.StringUtils;
import com.ww.editsms.widget.SearchEditText;
import com.ww.editsms.widget.XListView;
import com.ww.editsms.widget.XListView.IXListViewListener;

public class SearchActivity extends MyActivity implements IXListViewListener {
	XListView listView;
	SearchEditText search_Keywords;
	Button button_search;
	public static List<Person> list;
	private PersonAdapter adpter;
	List<String[]> all=new ArrayList<String[]>();
	int pageNo = 0;
	int pageSize = 20;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setTitle("号码搜索");

		listView = (XListView) findViewById(R.id.listView);
		listView.setPullLoadEnable(true);
		listView.setXListViewListener(this);
		listView.hideHeader();
		search_Keywords = (SearchEditText) findViewById(R.id.search_Keywords);
		search_Keywords.setHint("请输入查询号码");
		search_Keywords.setText(getIntent().getStringExtra("key"));
		button_search = (Button) findViewById(R.id.button_search);
		listView.setOnItemClickListener(click);
		button_search.setOnClickListener(butSearchClick);
		getPhoneContacts();
	}

	OnClickListener butSearchClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (StringUtils.isNotEmpty(search_Keywords.getText().toString())) {
				pageNo=0;
				list.clear();
				getPhoneContacts();
			} else {
				showMessage("请输入查询号码");
			}

		}
	};

	OnItemClickListener click = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			Intent intent = new Intent();
			Bundle mBundle = new Bundle();
			intent.putExtras(mBundle);
			intent.putExtra("image", list.get(position - 1).getPhoto());
			intent.putExtra("name", list.get(position - 1).getName());
			intent.putExtra("phone", list.get(position - 1).getNumber());
			intent.setClass(SearchActivity.this, SmsListActivity.class);
			startActivity(intent);
		}
	};

	public void getPhoneContacts() {
		showDialog(0);
		new Thread() {
			public void run() {
				List<String[]> temp = Cursordb.getAllPhone(SearchActivity.this);
				for(int i=0;i<temp.size();i++){
					if(temp.get(i)[0].contains(search_Keywords.getText().toString())){
						all.add(temp.get(i));
					}
				}
				list = getPage();
				handler.sendEmptyMessage(1);
			}
		}.start();
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				adpter = new PersonAdapter(SearchActivity.this, list);
				listView.setAdapter(adpter);
				if (all.size() < pageSize) {
					listView.hideFooter();
				}
				break;
			case 2:
				adpter.setList(list);
				adpter.notifyDataSetChanged();
				listView.hideFooter();
				listView.stopLoadMore();
				break;
			case 3:
				adpter.setList(list);
				adpter.notifyDataSetChanged();
				listView.stopLoadMore();
				break;

			default:
				break;
			}

		};
	};

	@Override
	public void onRefresh() {

	}

	private List<Person> getPage() {
		List<String[]> temp = new ArrayList<String[]>();
		pageNo++;
		int start = (pageNo - 1) * pageSize;
		int end = (pageNo) * pageSize;
		if (end >= all.size()) {
			end = all.size();
		}
		for (int i = start; i < end; i++) {
			temp.add(all.get(i));
		}
		List<Person> list = Cursordb.getPersonList(SearchActivity.this, temp);
		return list;
	}

	@Override
	public void onLoadMore() {
		List<Person> temp = getPage();
		list.addAll(temp);
		if (temp.size() < pageSize) {
			handler.sendEmptyMessage(2);
		} else {
			handler.sendEmptyMessage(3);
		}
	}

	@Override
	public void stopLoad() {

	}

	@Override
	public void getList() {

	}

}
